import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
var _excluded = ["organizationId"];
import request from 'utils/request';
import { CPAAS_FILE, CPAAS_MSG, CPAAS_PLATFORM, VERSION_IS_OP } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel, parseParameters } from 'utils/utils';

function prefixUrl(head, suffixUrl) {
  var isTenant = VERSION_IS_OP || isTenantRoleLevel();
  var organizationId = getCurrentOrganizationId();

  if (isTenant) {
    return "".concat(head, "/v1/").concat(organizationId, "/").concat(suffixUrl);
  }

  return "".concat(head, "/v1/").concat(suffixUrl);
}
/**
 * 查询公告列表数据
 * @param {Object} params - 查询参数
 * @param {String} [params.page = 0] - 页码
 * @param {String} [params.size = 0] - 页数
 */


export function fetchNotice(_x) {
  return _fetchNotice.apply(this, arguments);
}
/**
 * 创建公告基础信息
 * @async
 * @function fetchEmailData
 * @param {Object} params - 查询参数
 * @param {String} params.page - 页码
 * @param {String} params.size - 页数
 * @param {String} params.enabledFlag - 是否启用
 * @param {String} params.port - 端口
 * @param {String} params.sender - 发送人
 * @param {String} params.serverCode - 账户代码
 * @param {String} params.serverName - 账户名称
 * @param {String} params.serverId - 服务器ID
 */

function _fetchNotice() {
  _fetchNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var organizationId, other;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            organizationId = params.organizationId, other = _objectWithoutProperties(params, _excluded);
            return _context.abrupt("return", request(prefixUrl(CPAAS_MSG, 'notices'), {
              method: 'GET',
              query: other
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _fetchNotice.apply(this, arguments);
}

export function createNotice(_x2) {
  return _createNotice.apply(this, arguments);
}
/**
 * 修改公告基础信息
 * @async
 * @function fetchEmailData
 * @param {Object} params - 查询参数
 * @param {String} params.page - 页码
 * @param {String} params.size - 页数
 * @param {String} params.enabledFlag - 是否启用
 * @param {String} params.port - 端口
 * @param {String} params.sender - 发送人
 * @param {String} params.serverCode - 账户代码
 * @param {String} params.serverName - 账户名称
 * @param {String} params.serverId - 服务器ID
 * @param {String} params.host -邮件服务器
 * @param {String} params.tenantId - 租户ID
 * @param {String} params.tenantName - 租户名称
 * @param {String} params.tryTimes - 重试次数
 * @param {String} params.userName - 用户名称
 * @param {String} params.password - 密码
 * @param {Array} params.emailProperties - 服务器配置属性
 * @param {String} params.emailProperties.propertyCode - 属性编码
 * @param {String} params.emailProperties.propertyId - 属性ID
 * @param {String} params.emailProperties.propertyValue - 属性值
 * @param {String} params.emailProperties.serverId - 服务器ID
 * @param {String} params.emailProperties.tenantId - 租户ID
 */

function _createNotice() {
  _createNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request(prefixUrl(CPAAS_MSG, 'notices'), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _createNotice.apply(this, arguments);
}

export function updateNotice(_x3) {
  return _updateNotice.apply(this, arguments);
}
/**
 * 删除公告基础信息
 * @async
 * @function fetchEmailData
 * @param {Object} params - 查询参数
 * @param {String} params.page - 页码
 * @param {String} params.size - 页数
 * @param {String} params.enabledFlag - 是否启用
 * @param {String} params.port - 端口
 * @param {String} params.sender - 发送人
 * @param {String} params.serverCode - 账户代码
 * @param {String} params.serverName - 账户名称
 * @param {String} params.serverId - 服务器ID
 * @param {String} params.host -邮件服务器
 * @param {String} params.tenantId - 租户ID
 * @param {String} params.tenantName - 租户名称
 * @param {String} params.tryTimes - 重试次数
 * @param {String} params.userName - 用户名称
 * @param {String} params.password - 密码
 * @param {Array} params.emailProperties - 服务器配置属性
 * @param {String} params.emailProperties.propertyCode - 属性编码
 * @param {String} params.emailProperties.propertyId - 属性ID
 * @param {String} params.emailProperties.propertyValue - 属性值
 * @param {String} params.emailProperties.serverId - 服务器ID
 * @param {String} params.emailProperties.tenantId - 租户ID
 * @param {Array} params.permissionList = ['cmsg.notices.list.button.delete']
 */

function _updateNotice() {
  _updateNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request(prefixUrl(CPAAS_MSG, 'notices'), {
              method: 'PUT',
              body: params
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _updateNotice.apply(this, arguments);
}

export function deleteNotice(_x4) {
  return _deleteNotice.apply(this, arguments);
}
/**
 * 查询公告明细数据
 * @param {Object} params - 查询参数
 * @param {String} [params.page = 0] - 页码
 * @param {String} [params.size = 0] - 页数
 * @param {Array} params.permissionList = ['cmsg.notices.list.button.release2']
 */

function _deleteNotice() {
  _deleteNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request(prefixUrl(CPAAS_MSG, 'notices'), {
              method: 'DELETE',
              body: params
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _deleteNotice.apply(this, arguments);
}

export function queryNotice(_x5) {
  return _queryNotice.apply(this, arguments);
}
/**
 * 发布公告
 * @param {Object} params - 查询参数
 * @param {String} [params.page = 0] - 页码
 * @param {String} [params.size = 0] - 页数
 */

function _queryNotice() {
  _queryNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request(prefixUrl(CPAAS_MSG, "notices/".concat(params.noticeId)), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _queryNotice.apply(this, arguments);
}

export function publicNotice(_x6) {
  return _publicNotice.apply(this, arguments);
}
/**
 * 撤销删除公告
 * @param {Object} params - 查询参数
 * @param {String} [params.organizationId] - 租户ID
 * @param {String} [params.noticeId] - 公告ID
 * @param {Array} params.permissionList = ['cmsg.notices.list.button.revoke2']
 */

function _publicNotice() {
  _publicNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request(prefixUrl(CPAAS_MSG, "notices/".concat(params.noticeId, "/publish")), {
              method: 'POST',
              query: params
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _publicNotice.apply(this, arguments);
}

export function revokeNotice(_x7) {
  return _revokeNotice.apply(this, arguments);
}
/**
 * 富文本上传
 * @async
 * @function queryList
 * @param {object} params - 查询条件
 * @param {!string} [params.bucketName = 0] - 数据页码
 * @param {!string} [params.fileName = 10] - 分页大小
 * @returns {object} fetch Promise
 */

function _revokeNotice() {
  _revokeNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request(prefixUrl(CPAAS_MSG, "notices/".concat(params.noticeId, "/revoke")), {
              method: 'POST',
              body: params.record
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _revokeNotice.apply(this, arguments);
}

export function uploadImage() {
  return _uploadImage.apply(this, arguments);
} // FIXME: 没有使用公共的 service

function _uploadImage() {
  _uploadImage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
    var params,
        file,
        data,
        _args8 = arguments;
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            params = _args8.length > 0 && _args8[0] !== undefined ? _args8[0] : {};
            file = _args8.length > 1 ? _args8[1] : undefined;
            data = new FormData();
            Object.keys(params).forEach(function (paramKey) {
              data.append(paramKey, params[paramKey]);
            });
            data.append('file', file, file.name);
            return _context8.abrupt("return", request("".concat(CPAAS_FILE, "/v1/files/multipart"), {
              processData: false,
              // 不会将 data 参数序列化字符串
              method: 'POST',
              body: data,
              responseType: 'text'
            }));

          case 6:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _uploadImage.apply(this, arguments);
}

export function queryNoticeType(_x8) {
  return _queryNoticeType.apply(this, arguments);
}
/**
 * 删除公告接收记录
 * @returns {Promise<void>}
 */

function _queryNoticeType() {
  _queryNoticeType = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(params) {
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            return _context9.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/lovs/value/tree"), {
              method: 'GET',
              query: params
            }));

          case 1:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _queryNoticeType.apply(this, arguments);
}

export function removeDraftReceive(_x9) {
  return _removeDraftReceive.apply(this, arguments);
}
/**
 * 查询 历史发布信息
 */

function _removeDraftReceive() {
  _removeDraftReceive = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(record) {
    var requestUrl;
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            requestUrl = prefixUrl(CPAAS_MSG, "notices/receivers");
            return _context10.abrupt("return", request(requestUrl, {
              method: 'DELETE',
              body: record
            }));

          case 2:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _removeDraftReceive.apply(this, arguments);
}

export function querySystemHistory(_x10, _x11) {
  return _querySystemHistory.apply(this, arguments);
}
/**
 * 查询 历史发布信息 对应的接收配置
 * @returns {Promise<void>}
 */

function _querySystemHistory() {
  _querySystemHistory = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(noticeId, query) {
    var requestUrl;
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            requestUrl = prefixUrl(CPAAS_MSG, "notices/published-records/".concat(noticeId));
            return _context11.abrupt("return", request(requestUrl, {
              method: 'GET',
              query: parseParameters(query)
            }));

          case 2:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _querySystemHistory.apply(this, arguments);
}

export function queryReceiver(_x12, _x13, _x14) {
  return _queryReceiver.apply(this, arguments);
}
/**
 * 新增 接收配置
 * @returns {Promise<void>}
 */

function _queryReceiver() {
  _queryReceiver = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(noticeId, query, publishedIds) {
    var requestUrl;
    return _regeneratorRuntime.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            requestUrl = prefixUrl(CPAAS_MSG, "notices/receivers/".concat(noticeId));
            return _context12.abrupt("return", request(requestUrl, {
              method: 'GET',
              query: _objectSpread(_objectSpread({}, parseParameters(query)), {}, {
                publishedIds: publishedIds
              })
            }));

          case 2:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  }));
  return _queryReceiver.apply(this, arguments);
}

export function createReceiver(_x15, _x16) {
  return _createReceiver.apply(this, arguments);
}
/**
 * 发布系统消息公告
 * @returns {Promise<void>}
 */

function _createReceiver() {
  _createReceiver = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(noticeId, records) {
    var requestUrl;
    return _regeneratorRuntime.wrap(function _callee13$(_context13) {
      while (1) {
        switch (_context13.prev = _context13.next) {
          case 0:
            requestUrl = prefixUrl(CPAAS_MSG, "notices/receivers/".concat(noticeId));
            return _context13.abrupt("return", request(requestUrl, {
              method: 'POST',
              body: records
            }));

          case 2:
          case "end":
            return _context13.stop();
        }
      }
    }, _callee13);
  }));
  return _createReceiver.apply(this, arguments);
}

export function publishSystemNotice(_x17, _x18) {
  return _publishSystemNotice.apply(this, arguments);
}

function _publishSystemNotice() {
  _publishSystemNotice = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14(noticeId, records) {
    var requestUrl;
    return _regeneratorRuntime.wrap(function _callee14$(_context14) {
      while (1) {
        switch (_context14.prev = _context14.next) {
          case 0:
            requestUrl = prefixUrl(CPAAS_MSG, "notices/published-records/".concat(noticeId, "/publish"));
            return _context14.abrupt("return", request(requestUrl, {
              method: 'POST',
              body: records
            }));

          case 2:
          case "end":
            return _context14.stop();
        }
      }
    }, _callee14);
  }));
  return _publishSystemNotice.apply(this, arguments);
}